// Written by Steve Andersen, copyright (c) 2007 ONE/Northwest
// This program is released under the GNU General Public License. http://www.gnu.org/licenses/
trigger ONEN_ContactInsertHouseholding on Contact (after insert) {
	//system.debug('ONEN_ContactInsertHouseholding FIRED');
	
    //a list for all the Households to be inserted for new Contacts
    List<ONEN_Household__c> NewHouseholds = new List<ONEN_Household__c>();
    //a list of Contacts that don't have Households
    List<Contact> ContactsWithNoHouseholds = new List<Contact>();
    //a list of Names for Households for new Contacts
    List<String> ContactsWithNoHouseholdsHouseholdName = new List<String>();
    //a list of Greetings for Households for new Contacts
    List<String> ContactsWithNoHouseholdsGreeting = new List<String>();
    //a list of Contacts that have Households
    List<Contact> ContactsWithHouseholds = new List<Contact>();
    //a list of Contacts that have Households and have a valid address
    List<Contact> ContactsWithHouseholdsWithGoodAddress = new List<Contact>();
    //a list of Ids of Households already existing
    List<Id> ExistingHouseholdIds = new List<Id>();
    
    //get all the contacts in the trigger map
	for (Contact c : [Select Id, ONEN_Household__c, FirstName, LastName,OtherStreet,OtherCity,OtherState,OtherPostalCode,OtherCountry,Home_County__c,ONEN_Household__r.MailingStreet__c,ONEN_Household__r.MailingCity__c,ONEN_Household__r.MailingState__c,ONEN_Household__r.MailingPostalCode__c,ONEN_Household__r.MailingCountry__c,ONEN_Household__r.MailingCounty__c from Contact where Id IN :Trigger.newMap.keyset()]) {
		//system.debug(c);
		//if the household doesn't have a Household selected
		if (c.ONEN_Household__c == NULL) {
			//add it to the list of Contacts with no Household
	 		ContactsWithNoHouseholds.add(c);
	 	} else {
            
           	ContactsWithHouseholds.add(c);
			
            //add that Household Id to the list of all Households to be updated
            ExistingHouseholdIds.add(c.ONEN_Household__c);
        }
	} 
	
	if(ContactsWithNoHouseholds.size()>0||ContactsWithHouseholds.size()>0||ContactsWithHouseholdsWithGoodAddress.size()>0) {
		//system.debug('ONEN_ContactInsertHouseholding EXECUTED');
		//map for Existing Households to update
		Map<Id,ONEN_Household__c> ExistingHouseholds = new Map<Id,ONEN_Household__c>([Select Id,Name, Greeting__c, Override_Household_Name__c, Override_Greeting__c,MailingStreet__c,MailingCity__c,MailingState__c,MailingPostalCode__c,MailingCountry__c,MailingCounty__c, Member_Count__c from ONEN_Household__c where Id IN :ExistingHouseholdIds]); 
		ONEN_HouseholdMaintenance ThisHouseholdSet = new ONEN_HouseholdMaintenance(ExistingHouseholds);
		//get all the Contacts that don't have Households
		
		if(ContactsWithNoHouseholds.size()>0){
			ThisHouseholdSet.createNewHouseholds(ContactsWithNoHouseholds);			
			update ContactsWithNoHouseholds;
		}
		//map for new Household member counts
		Map<Id,Double> HouseholdAdditions = new Map<Id,Double>();
		
		Double CurrentHouseholdMemberCount;
		Double PreviousHouseholdMembershipTotal;
		CurrentHouseholdMemberCount=0;
		PreviousHouseholdMembershipTotal=0;

		//loop through the contacts that have Households
		for (Contact ContactInHousehold : ContactsWithHouseholds) { 
			//for this contact, get the hh member count from hh object
			CurrentHouseholdMemberCount=ExistingHouseholds.get(ContactInHousehold.ONEN_Household__c).Member_Count__c;
			//check to see if this hh is in the HouseholdAdditions map
			if(HouseholdAdditions.keySet().Contains(ContactInHousehold.ONEN_Household__c)) {
				//if so, increment the number
				PreviousHouseholdMembershipTotal=HouseholdAdditions.get(ContactInHousehold.ONEN_Household__c);
				HouseholdAdditions.put(ContactInHousehold.ONEN_Household__c,PreviousHouseholdMembershipTotal+1);
			} else {
				//if not, add it 
				Double Total;
				Total = CurrentHouseholdMemberCount + 1;
				//add that to the Map for updating
				HouseholdAdditions.put(ContactInHousehold.ONEN_Household__c,Total);
			}		
		}

		ONEN_Household__c ThisHH;
		
		//loop through Housheolds to have member count updated, and set the Member Count value
		for (Id HHId : HouseholdAdditions.keySet()) {
			ThisHH = ExistingHouseholds.get(HHId);
			ThisHH.Member_Count__c = HouseholdAdditions.get(HHId);
		}
		
		//if there are existing Households to update, go for it

		if(ExistingHouseholds.size()>0){
			update ExistingHouseholds.values();
		}
	}
}